/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.wholeconsulting.pacc.gui.utils.treetable.generic;

import ar.com.wholeconsulting.pacc.gui.utils.treetable.PACCAbstractTreeTableModel;
import ar.com.wholeconsulting.pacc.gui.utils.treetable.TreeTableNode;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.util.EventObject;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import javax.swing.DefaultCellEditor;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.CellEditorListener;
import javax.swing.table.TableCellEditor;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreePath;
import org.jdesktop.swingx.JXTreeTable;

/**
 *
 * @author pable
 */
public class EachElementTableEditor implements TreeCellEditor, TableCellEditor {

    protected Map<String, DefaultCellEditor> editors;
    protected DefaultCellEditor editor, defaultEditor;
    PACCAbstractTreeTableModel model;
    JXTreeTable tree;

    /**
     * Constructs a EachRowEditor. create default editor
     *
     * @see TableCellEditor
     * @see DefaultCellEditor
     */
    public EachElementTableEditor(PACCAbstractTreeTableModel model, JXTreeTable tree) {
        this.model = model;
        this.tree = tree;
        editors = new HashMap<String, DefaultCellEditor>();
        defaultEditor = new DefaultCellEditor(new JTextField());
    }

    @Override
    public Object getCellEditorValue() {
        return editor.getCellEditorValue();
    }

    @Override
    public boolean stopCellEditing() {
        return editor.stopCellEditing();
    }

    @Override
    public void cancelCellEditing() {
        editor.cancelCellEditing();
    }

    @Override
    public boolean isCellEditable(EventObject anEvent) {
        selectEditor(anEvent);
        return editor.isCellEditable(anEvent);
    }

    @Override
    public void addCellEditorListener(CellEditorListener l) {
        editor.addCellEditorListener(l);
    }

    @Override
    public void removeCellEditorListener(CellEditorListener l) {
        editor.removeCellEditorListener(l);
    }

    @Override
    public boolean shouldSelectCell(EventObject anEvent) {
        selectEditor((MouseEvent) anEvent);
        return editor.shouldSelectCell(anEvent);
    }

    protected void selectEditor(EventObject e) {
        TreeTableNode node;
        TreePath tp = null;
        // or use getSelectionPaths() for all
        if (e == null || !(e instanceof MouseEvent)) {
            int row = tree.getSelectionModel().getAnchorSelectionIndex();
            tp = tree.getPathForRow(row);
        } else  {
            MouseEvent ev=(MouseEvent)e;
            tp =tree.getPathForLocation(ev.getX(), ev.getY());
        }
        node = (TreeTableNode) tp.getLastPathComponent();
        editor = (DefaultCellEditor) editors.get(node.getName());
        if (editor == null) {
            editor = defaultEditor;
        }
    }

    @Override
    public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
        return editor.getTreeCellEditorComponent(tree, value, isSelected, expanded, leaf, row);
    }

    public Map<String, DefaultCellEditor> getEditors() {
        return editors;
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        return editor.getTableCellEditorComponent(tree, value, isSelected, row, column);
    }
}
